package lex.mtd.utils;

import java.util.logging.*;

import org.lex.utils.logging.PatternFormatter;


public class Logs
{
	private static final Level DEFAULT_LEVEL = Level.ALL;
	private static Level currentLevel = DEFAULT_LEVEL;
	private static Handler handler = null;
	private static Formatter formatter = null;
	// -----------------------------------------------
	private static final String ROOT = "lex.mtd";

	public static Logger get( Class<?> o )
	{
		if( null == o )
			return get( ROOT );
		else
			return get( o.getName() );
	}

	public static Logger get( String name )
	{
		if( null == name )
			name = ROOT;
		Logger log = LogManager.getLogManager().getLogger( name );
		if( null == log )
		{
			log = Logger.getLogger( name );
			if( log.getName().startsWith( ROOT ) )
			{
				log.setUseParentHandlers( false );
				log.addHandler( getHandler() );
				log.setLevel( getLevel() );
			}
		}
		// config log
		return log;
	}

	public static Level getLevel()
	{
		return currentLevel;
	}

	public static Handler getHandler()
	{
		if( null == handler )
		{
			handler = new ConsoleHandler();
			handler.setLevel( getLevel() );
			handler.setFormatter( getFormatter() );
		}
		return handler;
	}

	public static Formatter getFormatter()
	{
		if( null == formatter )
		{
			formatter = new PatternFormatter();
		}
		return formatter;
	}

	public static void main( String[] args )
	{
		Logs.get( Logs.class ).fine( "message" );
	}
}
